
package com.shop.cloud.upms.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.constant.CacheConstants;
import com.shop.cloud.common.core.constant.CommonConstants;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.common.log.annotation.SysLog;
import com.shop.cloud.upms.common.entity.SysTenantApply;
import com.shop.cloud.upms.admin.service.SysTenantApplyService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 租户入驻申请
 *
 * @author
 * @date 2023-12-12 14:47:16
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/tenantapply")
@Tag(description = "tenantapply", name = "租户入驻申请管理")
public class SysTenantApplyController {

    private final SysTenantApplyService sysTenantApplyService;
    private final RedisTemplate redisTemplate;

    /**
     * 租户入驻申请分页列表
     * @param page 分页对象
     * @param sysTenantApply 租户入驻申请
     * @return
     */
    @Operation(summary = "租户入驻申请分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ato.hasAuthority('upms:tenantapply:index')")
    public R getPage(Page page, SysTenantApply sysTenantApply) {
        return R.ok(sysTenantApplyService.page(page, Wrappers.query(sysTenantApply)));
    }

    /**
     * 租户入驻申请查询
     * @param id
     * @return R
     */
    @Operation(summary = "租户入驻申请查询")
    @GetMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('upms:tenantapply:get')")
    public R getById(@PathVariable("id") String id) {
        return R.ok(sysTenantApplyService.getById(id));
    }

    /**
     * 租户入驻申请删除
     * @param id
     * @return R
     */
    @Operation(summary = "租户入驻申请删除")
    @SysLog("删除租户入驻申请")
    @DeleteMapping("/{id}")
    @PreAuthorize("@ato.hasAuthority('upms:tenantapply:del')")
    public R removeById(@PathVariable String id) {
        return R.ok(sysTenantApplyService.removeById(id));
    }

    /**
     * 租户入驻申请审核
     * @param sysTenantApply 租户入驻申请表
     * @return R
     */
    @Operation(summary = "租户入驻申请审核")
    @PutMapping("/verify")
    @PreAuthorize("@ato.hasAuthority('upms:tenantapply:verify')")
    public R verify(@RequestBody SysTenantApply sysTenantApply) {
        try{
            sysTenantApplyService.verify(sysTenantApply);
        }catch (Exception e){
            if(e.getMessage().contains("uk_phone")){
                return R.failed("该手机号已存在");
            }if(e.getMessage().contains("uk_username")){
                return R.failed("该账号已存在");
            }if(e.getMessage().contains("uk_email")){
                return R.failed("该邮箱已存在");
            }else{
                return R.failed(e.getMessage());
            }
        }
        return R.ok();
    }

    /**
     * 租户入驻申请修改
     * @param sysTenantApply 租户入驻申请
     * @return R
     */
    @Operation(summary = "租户入驻申请修改")
    @SysLog("修改租户入驻申请")
    @PutMapping("/addedit")
    public R updateById(@RequestBody SysTenantApply sysTenantApply) {
        if(StrUtil.isNotBlank(sysTenantApply.getId())){
            SysTenantApply sysTenantApply2 = sysTenantApplyService.getById(sysTenantApply.getId());
            if("1".equals(sysTenantApply2.getStatus())){
                return R.failed("该状态不支持修改");
            }
            sysTenantApplyService.updateById(sysTenantApply);
        }else{
            //验证码合法校验
            String key = CacheConstants.VER_CODE_DEFAULT + CommonConstants.PHONE_CODE_6 + ":" + sysTenantApply.getUserPhone();
            Object codeObj = redisTemplate.opsForValue().get(key);
            if (codeObj == null) {
                return R.failed("验证码不合法");
            }
            String saveCode = codeObj.toString();
            if (StrUtil.isBlank(saveCode) || !StrUtil.equals(saveCode, sysTenantApply.getSmsCode())) {
                redisTemplate.delete(key);
                return R.failed("验证码不合法");
            }
            sysTenantApply.setStatus(null);
            sysTenantApplyService.save(sysTenantApply);
        }
        return R.ok(sysTenantApply);
    }

    /**
     * 租户入驻申请查询
     * @param sysTenantApply
     * @return R
     */
    @Operation(summary = "租户入驻申请查询")
    @GetMapping("/one")
    public R getOne(SysTenantApply sysTenantApply) {
        return R.ok(sysTenantApplyService.getById(sysTenantApply.getId()));
    }
}
